﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Security;
using System.Security.Cryptography;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
namespace PM.Code.Web
{
    /// <summary>
    /// 安全safety类
    /// </summary>
    public class safety
    {
        public safety()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        
        /// <summary>
        /// 生成权值
        /// </summary>
        ///<returns>权值</returns>
        public static string get_GenerateSalt()
        {
            byte[] data = new byte[0x10];
            new RNGCryptoServiceProvider().GetBytes(data);
            return Convert.ToBase64String(data);
        }

        /// <summary>
        /// 加密密码
        /// </summary>
        /// <param name="password">密码</param>
        /// <param name="salt">权值</param>
        /// <returns>返回加密密码</returns>
        public static string EncodePassword(string password, string salt)
        {
            byte[] buffer3;
            byte[] buffer4;
            byte[] bytes = Encoding.Unicode.GetBytes(password);
            byte[] src = Convert.FromBase64String(salt);
            buffer3 = new byte[src.Length + bytes.Length];
        
            buffer4 = null;
            Buffer.BlockCopy(src, 0, buffer3, 0, src.Length);
            Buffer.BlockCopy(bytes, 0, buffer3, src.Length, bytes.Length);
           
            buffer4 = HashAlgorithm.Create("SHA1").ComputeHash(buffer3);
            return Convert.ToBase64String(buffer4);
        }
    }
}
